﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AiFrame.InterfaceLib;
using AiFrame.InterfaceLib.MVP;
using AiFrame.InterfaceLib.Windows.UI;

namespace AiFrame.Base.WinUI.Windows.UI
{
    class ToolBarManager: IToolBarManager
    {
        private List<ToolStrip> _consistentGroups;
        private List<ToolStrip> _hideableGroups;
        private static ToolBarManager _instance;
        private ToolStripPanel _toolParent;
        private IWindowManager<IWindow> _windowManager;


        private ToolBarManager()
        {
            _consistentGroups = new List<ToolStrip>();
            _hideableGroups = new List<ToolStrip>();
            
        }

        public static ToolBarManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ToolBarManager();

                return _instance;
            }
        }

        public void Init(ToolStripPanel toolParent, IWindowManager<IWindow> windowManager)
        {
            _windowManager = windowManager;
            _windowManager.ActiveWindowChanges += new EventHandler<ActiveWindowChangedParams<IWindow>>(_windowManager_ActiveWindowChanges);

            ToolParent = toolParent;
        }

        void _windowManager_ActiveWindowChanges(object sender, ActiveWindowChangedParams<IWindow> e)
        {
            /* Alle anderen Groups vorher ausblenden */
            foreach (ToolStrip strip in _hideableGroups)
            {
                strip.Parent = null;
            }
            /* Groups aus Liste löschen */
            _hideableGroups.Clear();

            
        }
        
        /// <summary>
        /// Setzt das Parent-Control für die Tool-Items oder
        /// gibt es zurück.
        /// </summary>
        public ToolStripPanel ToolParent
        {
            get { return _toolParent; }
            set
            {
                _toolParent = value;
                
            }
        }

        #region Implementation of IToolBarManager<ToolStrip>

        public void AddToolGroup(object group, bool showAlways)
        {
            if (showAlways)
                AddToolGroup(group, showAlways, 0);
            else
                AddToolGroup(group, showAlways, 1);
        }

        public void AddToolGroup(object group, bool showAlways, int row)
        {
            ToolStrip toolStrip;
            if (group is ToolStrip)
                toolStrip = (ToolStrip)group;
            else
                throw new InvalidTypeException("Der ToolBarManager kann nur ToolStrips aufnehmen.");

            if (showAlways) 
                _consistentGroups.Add(toolStrip);
            else
                _hideableGroups.Add(toolStrip);


            _toolParent.Join(toolStrip, row);

        }

        #endregion
    }
}
